package com.pzhu.acargid.coauthor;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.pzhu.acargid.domain.Coauthor;
import com.pzhu.acargid.domain.Doc;
import com.pzhu.acargid.domain.Institution;
import com.pzhu.acargid.institution.InstitutionService;
import com.pzhu.acargid.until.Result;
import com.pzhu.file.utils.FileUtil;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class CoauthorUtil {
    private final CoauthorService coauthorService;
    private final InstitutionService institutionService;

    public CoauthorUtil(CoauthorService coauthorService, InstitutionService institutionService) {
        this.coauthorService = coauthorService;
        this.institutionService = institutionService;
    }


    public Result getResult(List<Coauthor> coauthors, Doc doc, Integer userid) {
        if (coauthors != null) {
            coauthors.forEach(coauthor -> {
                coauthor.setDocId(doc.getId());
                coauthor.setUserId(userid);
            });
//            coauthorService.saveBatch(coauthors);
        }
   /*     coauthors.forEach(coauthor -> coauthor.getInstitutions().forEach(institution -> institution.setAuthorId(coauthor.getId())));
        List<Institution> collect = coauthors.stream().map(Coauthor::getInstitutions).collect(Collectors.toList())
                .stream().flatMap(Collection::stream).collect(Collectors.toList());
        institutionService.saveBatch(collect);*/
        return Result.success(null);
    }

    public Result update(List<Coauthor> coauthors, Doc doc, Integer userid) throws IOException {

//        List<Integer> collect1 = coauthors.stream().map(Coauthor::getId).collect(Collectors.toList());
//
//        LambdaQueryWrapper<Institution> institutionLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        institutionLambdaQueryWrapper.in(!collect1.isEmpty(), Institution::getAuthorId, collect1);
//        institutionService.remove(institutionLambdaQueryWrapper);
//        LambdaQueryWrapper<Coauthor> coauthorLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        coauthorLambdaQueryWrapper.eq(Coauthor::getDocId, doc.getId());
//        coauthorService.remove(coauthorLambdaQueryWrapper);
//        String savePath = FileUtil.getSavePath(userid, type);
//        FileUtils.cleanDirectory(new File(savePath));
        return getResult(coauthors, doc, userid);
    }
}
